﻿using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;

namespace TripLOA.WebService
{
    /// <summary>
    /// Service class. Contains methods for object serialization and string deserialization
    /// </summary>
    public class Serialization
    {
        /// <summary>
        /// Serialize an object in json format or xml format.
        /// </summary>
        /// <param name="obj">Object to serialize</param>
        /// <param name="type">Type of the object.</param>
        /// <param name="format">Format of the serialization</param>
        /// <returns></returns>
        public static string serialize (object obj, Type type, string format) 
        {
            MemoryStream ms = new MemoryStream();
            StreamReader sr;
            switch (format) 
            {
                case "json":
                    /*
                    Serializer serializer = new Serializer(obj.GetType());
                    serializer.Serialize( obj, output);
                    */

                    DataContractJsonSerializer json_serializer = new DataContractJsonSerializer(type);
                    
                    json_serializer.WriteObject(ms, obj);
                    ms.Position = 0;
                    sr = new StreamReader(ms);
                    break;
                default:
                    /*
                    XmlSerializer serializerXml = new XmlSerializer(obj.GetType());
                    serializerXml.Serialize(output, obj);
                    */
                    DataContractSerializer xml_serializer = new DataContractSerializer(type);
                    xml_serializer.WriteObject(ms, obj);
                    ms.Position = 0;
                    sr = new StreamReader(ms);
                    break;
            }
            return sr.ReadToEnd();
        }

        /// <summary>
        /// Deserialize a json or xml string in an object.
        /// </summary>
        /// <param name="str">String to deserialize</param>
        /// <param name="type">Type of the originary object.</param>
        /// <param name="format">String format: can be json or xml</param>
        /// <returns></returns>
        public static Object deserialize (string str, Type type, string format)
        {
            Object output;
            MemoryStream ms = new MemoryStream();
            StreamWriter sw = new StreamWriter(ms);
            sw.Write(str);
            sw.Flush();
            ms.Position = 0;

            switch (format)
            {
                case "json":
                    DataContractJsonSerializer json_serializer = new DataContractJsonSerializer(type);
                    output = json_serializer.ReadObject(ms);
                    break;

                default: //XML
                    DataContractSerializer xml_serializer = new DataContractSerializer(type);
                    output = xml_serializer.ReadObject(ms);
                    break;
            }

            return output;
        }

     
        public string SoapSerialize()
        {
            MemoryStream ms = new MemoryStream();
            return null;
        }
    }
}
